﻿/*
 *  Copyright 2009 Duck Pond, Inc.
 *  http://www.duckpondinc.com/
 *  
 *  This file is part of Server Watch & Alert.
 *
 *  Server Watch & Alert is free software: you can redistribute it and/or modify
 *  it under the terms of the GNU General Public License as published by
 *  the Free Software Foundation, either version 2 of the License, or
 *  (at your option) any later version.
 *
 *  Server Watch & Alert is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *  GNU General Public License for more details.
 *
 *  You should have received a copy of the GNU General Public License
 *  along with Server Watch & Alert.  If not, see <http://www.gnu.org/licenses/>.
 *  
 * */

using System;
using System.IO;
using System.Xml.Serialization;
using DuckPond.ServerWatchAlert.Core.ResultActions;
using DuckPond.ServerWatchAlert.Core.TestActions;

namespace DuckPond.ServerWatchAlert.Core.Xml
{
    [XmlRoot(ElementName = "config", Namespace = Config.XmlNamespace, IsNullable = false)]
    public class Config
    {
        /////////////////////
        // CONSTANTS
        /////////////
        public const string XmlNamespace = @"http://duckpondinc.com/ServerWatchAlert/Config";


        /////////////////////
        // PROPERTIES
        /////////////
        [XmlArray(ElementName = "result-actions")]
        [XmlArrayItem(ElementName = "email-result", Type = typeof(SendMailAction))]
        public ResultAction[] ResultActions { get; set; }

        [XmlArray(ElementName = "test-actions")]
        [XmlArrayItem(ElementName = "http-regex-test", Type = typeof(HttpRegexTest))]
        public TestAction[] TestActions { get; set; }


        /////////////////////
        // METHODS
        /////////////
        public static Config Load(string path)
        {
            XmlSerializer deserializer 
                = new XmlSerializer(
                    typeof(Config),
                    new Type[]
                    {
                        typeof(SendMailAction),
                        typeof(HttpRegexTest),
                    });

            Config config = null;
            using (StreamReader r = new StreamReader(path))
            {
                config = (Config)deserializer.Deserialize(r);
            }

            return config;
        }
    }
}
